//
//  SMRecomendViewController.swift
//  STMusic
//
//  Created by stoneobs on 17/3/21.
//  Copyright © 2017年 stoneobs. All rights reserved.
// 
//**********************这是一个容器，包含推荐，最新，话题三个子控制器***************************

import UIKit

class YYRecomendViewController: UIViewController,UIScrollViewDelegate{
    
    var scrollView = UIScrollView.init();
    
    var segment:STSegementButton?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = YYThemeStyleManger.defultManger.gradualColor(frame: self.view.bounds);

        let titleLable = STLabel.init(frame: .init(x: 0, y: 0, width: 40, height: 18), text: "余音", textColor: .white, font: 18, isSizetoFit: false, textAlignment: .center);
        titleLable?.backgroundColor = .clear;
        self.navigationItem.titleView = titleLable;
        
        //self.setRightItemWithTitle("播放按钮", titleColor: .white);
       
        self.automaticallyAdjustsScrollViewInsets = false;
        //segemant
        self.segment = STSegementButton.init(frame: .init(x: 0, y: 0, width: SCREEN_WIDTH - 30, height: 40), andTitle: ["推荐","最新","话题"], handle: { (but) -> Void in

            self.scrollView.setContentOffset(CGPoint.init(x: SCREEN_WIDTH * CGFloat((but?.tag)!), y: 0), animated: true);
        });
        self.segment?.butTitleColor = ThirdTextColor;
        self.segment?.lineColor = ThirdTextColor;
        self.segment?.backgroundColor = YYThemeStyleManger.defultManger.gradualColor(frame: (self.segment?.bounds)!);
        self.segment?.butTitleSelectedColor = .white;
        self.segment?.lineSelectedColor = .white;
        self.segment?.autoMoveWithClic = false;
        self.segment?.isShowSpringAnimation = false;
        self.segment?.centerX = SCREEN_WIDTH / 2;
        self.view.addSubview(self.segment!);
        
        //scrollview
        
        self.scrollView = UIScrollView.init(frame: .init(x: 0, y: 40, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - 44 - 64));
        self.scrollView.backgroundColor = YYThemeStyleManger.defultManger.gradualColor(frame: self.scrollView.bounds);
        self.scrollView.contentSize = .init(width: SCREEN_WIDTH * 3, height: 0);
        self.scrollView.delegate = self;
        self.scrollView.isPagingEnabled = true;
        self.scrollView.isScrollEnabled = true;
        STLayoutViewController.defaultLayout().panGestureRecognizer.require(toFail: self.scrollView.panGestureRecognizer);
        self.view.addSubview(self.scrollView);
        //child
        let recomendVC:YYChildRecomendController = YYChildRecomendController.init();
        self.scrollView.addSubview(recomendVC.view);
        self.addChildViewController(recomendVC);
        
        let chiledNewVC:YYChildNewViewController = YYChildNewViewController.init();
        chiledNewVC.view.left = SCREEN_WIDTH;
        self.scrollView.addSubview(chiledNewVC.view);
        self.addChildViewController(chiledNewVC);
        
        let chiledTopicVC:YYChildTopicViewController = YYChildTopicViewController.init();
        chiledTopicVC.view.left = SCREEN_WIDTH * 2;
        self.scrollView.addSubview(chiledTopicVC.view);
        self.addChildViewController(chiledTopicVC);

        
        //
//        var musicView = YYMusicAnimationView.init(frame: .init(x: 0, y: 0, width: 40, height: 30), style: .YYMusicAnimationViewStyleMiddleHigh);
//        musicView.itemColor = UIColor.flatPink;
        //musicView.startAnimation();

        
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated);
        YYDefaultMusicAnimationView.sharedInstance.isHidden = false;

    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated);
        YYDefaultMusicAnimationView.sharedInstance.isHidden = false;
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated);
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView == self.scrollView {
            let butwidth = SCREEN_WIDTH / 3 - 10;
            self.segment?.lineSelectedView.centerX =  butwidth/2 + self.scrollView.contentOffset.x/SCREEN_WIDTH * butwidth;
        }

    }
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        if scrollView == self.scrollView {
            let num = self.scrollView.contentOffset.x / SCREEN_WIDTH;
            self.segment?.cureentIndex = Int(num);
        }

    }
        // MARK:Action Method
    override func rightBarAction(_ sender: Any!) {
        STLayoutViewController.defaultLayout().showRightViewController(true);
    }


}
